import { store } from "@/store";
import { defineStore } from "pinia";
import { refreshType, IrefreshFunc } from "./types";

export const useAppStore = defineStore({
  id: "oms-refresh",
  state: (): refreshType => ({
    refreshFunc: null
  }),
  getters: {
    getRefrshFunc(state) {
      return state.refreshFunc;
    }
  },
  actions: {
    // 设置页面的刷新方法
    setRefrshFunc(refreshFunc: IrefreshFunc) {
      this.refreshFunc = refreshFunc;
    },
    // 清除页面的刷新方法
    clearRefrshFunc() {
      this.refreshFunc = null;
    },
    // 刷新数据
    refreshPage() {
      if (this.refreshFunc) {
        this.refreshFunc();
      } else {
        // TODO 需要后续根据需求做一些异常处理
        console.log("no func can execute...");
      }
    }
  }
});

export function useAppStoreHook() {
  return useAppStore(store);
}
